<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/libs/layui-v2.5.5/css/layui.css}" media="all">
    <link media="all" rel="stylesheet" th:href="@{/libs/font-awesome-4.7.0/css/font-awesome.min.css}">
    <link media="all" rel="stylesheet" th:href="@{/css/eleTree.css}">
    <link rel="stylesheet" th:href="@{/css/public.css}" media="all">
</head>
<body>
<div class="layui-row layui-col-space10">
    <div class="layui-col-md3">
        <div class="layuimini-container">
            <fieldset class="table-search-fieldset">
                <label>
                    部门信息
                    &nbsp&nbsp&nbsp&nbsp&nbsp
                    <a class="btn btn-box-tool" href="#" id="btnExpand" title="展开" type="button"><i
                            class="fa fa-chevron-up"></i></a>
                    <a class="btn btn-box-tool" href="#" id="btnCollapse" style="display:none;" title="折叠"
                       type="button"><i class="fa fa-chevron-down"></i></a>
                    <a class="btn btn-box-tool" href="#" id="btnRefresh" title="刷新部门信息" type="button"><i
                            class="fa fa-refresh"></i></a>

                </label>
                <hr class="layui-bg-gray">
                <div class="layui-row">
                    <div id="tree" lay-filter="chick"></div>
                </div>
            </fieldset>
        </div>
    </div>
    <div class="layui-col-md9">
        <div class="layuimini-container">
            <div class="layuimini-main">
                <fieldset class="table-search-fieldset">
                    <legend>搜索信息</legend>
                    <div style="margin: 10px 10px 10px 10px">
                        <form action="" class="layui-form layui-form-pane">
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">ID</label>
                                    <div class="layui-input-inline">
                                        <input autocomplete="off" class="layui-input" name="u_id" type="text">
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">用户姓名</label>
                                    <div class="layui-input-inline">
                                        <input autocomplete="off" class="layui-input" name="name" type="text">
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">用户性别</label>
                                    <div class="layui-input-inline">
                                        <input autocomplete="off" class="layui-input" name="sex" type="text">
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <button class="layui-btn layui-btn-primary" lay-filter="data-search-btn" lay-submit
                                            type="submit"><i class="layui-icon"></i> 搜 索
                                    </button>
                                </div>
                            </div>
                        </form>
                    </div>
                </fieldset>

                <script id="toolbarDemo" type="text/html">
                    <div class="layui-btn-container">
                        <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加
                        </button>
                        <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除
                        </button>
                        <button class="layui-btn layui-btn-sm  layui-btn-warm data-role-btn" lay-event="role"> 绑定角色
                        </button>
                        <button class="layui-btn layui-btn-sm  data-role-btn" lay-event="depart"> 绑定部门
                        </button>
                    </div>
                </script>

                <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

                <script id="currentTableBar" type="text/html">
                    <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
                    <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
                </script>
            </div>
        </div>
    </div>
</div>
<script th:src="@{/libs/layui-v2.5.5/layui.js}" charset="utf-8"></script>
<script charset="utf-8" th:src="@{/js/lay-config.js}"></script>
<script type="text/javascript" th:inline="none">
    layui.use(['form', 'table', 'eleTree'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            eleTree = layui.eleTree;

        $._tree = eleTree.render({
            elem: '#tree',
            highlightCurrent: true,
            expandOnClickNode: false,
            showLine: true,
            method: "get",      // 接口http请求类型
            url: "/system/depart/departTree",            // 异步接口地址
            response: {         // 对后台返回的数据格式重新定义
                statusCode: 200,
            },
            request: {          // 对于后台数据重新定义名字
                name: "name",
                key: "id",
                children: "children",
            },
        });

        eleTree.on("nodeClick(chick)", function (d) {
            departId = d.data.currentData.id
            table.reload('currentTableId', {
                url: '/system/user/listPage/' + departId,
                page: 1
            });
        })

        table.render({
            elem: '#currentTableId',
            url: '/system/user/listPage',
            toolbar: '#toolbarDemo',
            response: {
                statusCode: 200
            },
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'uid', title: 'ID', sort: true},
                {field: 'name', title: '用户名'},
                {field: 'sex', title: '性别'},
                {field: 'dateBirth', title: '出生日期'},
                {field: 'tel', title: '联系方式'},
                {field: 'roleNames', title: '担任角色'},
                {field: 'departName', title: '所属部门'},
                {field: 'createBy', align: 'center', title: '创建人'},
                {field: 'createTime', align: 'center', title: '创建时间'},
                {field: 'remark', title: '备注'},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line',
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);

            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    searchParams: result
                }
            }, 'data');

            return false;
        });

        $('#btnExpand').click(function () {
            $._tree.expandAll();
            $(this).hide();
            $('#btnCollapse').show();
        });

        $('#btnCollapse').click(function () {
            $._tree.unExpandAll();
            $(this).hide();
            $('#btnExpand').show();
        });

        $('#btnRefresh').click(function () {
            $.ajax({
                type: "GET",
                url: "/system/depart/departTree",
                dataType: "json",
                success: function (data) {
                    layer.alert(data.message);
                    if (data.code === 200) {
                        $._tree = $._tree.reload(data.data);
                    }
                }
            });
            $('#btnExpand').show();
            $('#btnCollapse').hide();
        });

        /**
         * toolbar监听事件
         */
        $_table = table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加用户',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/system/user/add',
                    end: function () {
                        $(".layui-laypage-btn")[0].click()
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                let checkStatus = table.checkStatus('currentTableId')
                let data = checkStatus.data;
                let ids = [];
                data.forEach(obj => {
                    ids.push(obj.uid)
                })
                if (ids.length > 0) {
                    layer.alert("是否确认删除", {
                        title: '最终的提交信息'
                    }, function () {
                        $.ajax({
                            type: "DELETE",
                            url: "/system/user/delete",
                            data: {
                                "ids": ids
                            },
                            dataType: "json",
                            success: function (data) {
                                layer.alert(data.message);
                                $(".layui-laypage-btn")[0].click()
                            }
                        });
                    });
                } else {
                    layer.alert("请先选中至少一名用户!");
                }
            } else if (obj.event === 'role') {  // 监听绑定角色操作
                let checkStatus = table.checkStatus('currentTableId')
                let data = checkStatus.data;
                let ids = [];
                data.forEach(obj => {
                    ids.push(obj.uid)
                })
                if (ids.length > 0) {
                    var index = layer.open({
                        title: '绑定角色',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: '/system/role/binding/' + ids,
                        end: function () {
                            $(".layui-laypage-btn")[0].click()
                        }
                    });
                } else {
                    layer.alert("请先选中至少一名用户!");
                }
            } else if (obj.event === 'depart') {  // 监听绑定角色操作
                let checkStatus = table.checkStatus('currentTableId')
                let data = checkStatus.data;
                let ids = [];
                data.forEach(obj => {
                    ids.push(obj.uid)
                })
                if (ids.length > 0) {
                    var index = layer.open({
                        title: '绑定部门',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: '/system/depart/binding/' + ids,
                        end: function () {
                            $(".layui-laypage-btn")[0].click()
                        }
                    });
                } else {
                    layer.alert("请先选中至少一名用户!");
                }
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                var index = layer.open({
                    title: '编辑用户',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/system/user/edit/' + data.uid,
                    end: function () {
                        $(".layui-laypage-btn")[0].click()
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    obj.del();
                    layer.close(index);
                });
            }
        });
    })
    ;
</script>
</body>
</html>